from project import test_dotnet_dump
from project import run_webapp
import configparser
import os
import sys
from Utils import run_command_sync, work_logger
from project import create_consoleapp, create_webapp


@work_logger(os.path.join(os.getcwd(), f'Tips\{__name__}.log'))
def prepare_test(log_path):
    shell_name = 'powershell.exe'
    command = 'dotnet --info'
    run_command_sync(command)
    rt_code = run_command_sync(
        ' '.join([f'{shell_name} {command}']), log_path=log_path)
    if rt_code == 0:
        message = 'Go ahead the program\n'
    else:
        message = 'SDK is not installed yet.Please run "analyzeRunTest.py" first\n'
    print(message)
    with open(log_path, 'a+')as log:
        log.write(message)
    if rt_code != 0:
        sys.exit(-1)


if __name__ == '__main__':

    # 读取配置文件
    config_file = os.path.join(os.getcwd(), 'Tips\config.conf')
    config = configparser.ConfigParser()
    config.read(config_file)
    sdk_version = config['SDK']['Version'][0]
    # prepare_test()
    TestFile = os.path.join(os.getcwd(), config['Test']['TestFile'])
    source_feed = config['SDK']['Feed']
    create_consoleapp(TestFile, sdk_version, source_feed)
    create_webapp(TestFile, source_feed)
    project_dir = os.path.join(TestFile, 'webapp')
    # run_webapp(project_dir)# Error：端口占用处理
    test_dotnet_dump(TestFile)  
